﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeresyManager.Comunication;

namespace HeresyManager.Careers
{
    [Serializable]
    public class Cleric : Career
    {

        private Skill[] skills;
        private Talent[] talents;


        public override string Name
        {
            get { return "Cleric"; }
        }

        public override IEnumerator<Comunicator> Acire(Character c)
        {

            List<Skill> skillList = new List<Skill>();

            skillList.Add(new Skills.SpeakLanguages.SpeakLanguageLowGothic(Skill.SkillLevel.Skilled));
            skillList.Add(new Skills.CommonLores.CommonLoreImperialCreed(Skill.SkillLevel.Skilled));
            skillList.Add(new Skills.Literacy(Skill.SkillLevel.Skilled));

            var skillChooser = new Comunication.ChooseOne<Skill>("Choose One", new Skills.Performers.PerformerSinger(Skill.SkillLevel.Skilled), new Skills.Trades.TradeCopyist(Skill.SkillLevel.Skilled));
            while (skillChooser.SelectedValue != null)
                yield return skillChooser;
            skillList.Add(skillChooser.SelectedValue);


            skillChooser = new Comunication.ChooseOne<Skill>("Choose One", new Skills.Trades.TradeCook(Skill.SkillLevel.Skilled), new Skills.Trades.TradeValet(Skill.SkillLevel.Skilled));
            while (skillChooser.SelectedValue != null)
                yield return skillChooser;
            skillList.Add(skillChooser.SelectedValue);

            skills = skillList.ToArray();

            foreach (var s in skills)
            {
                var en = c.AddSkill(s, this);
                while (en.MoveNext())
                    yield return en.Current;
            }




            List<Talent> talentList = new List<Talent>();

            talentList.Add(new Talents.WeaponTraining.MeleeWeaponTrainingPrimitive());
            talentList.Add(new Talents.WeaponTraining.PistolTrainingSP());

            var choose = new ChooseOne<Talent>("Choose One", new Talents.WeaponTraining.BasicWeaponTrainingPrimitive(), new Talents.WeaponTraining.ThrownWeaponTrainingPrimitive());
            while (choose.SelectedValue == null)
                yield return choose;
            talentList.Add(choose.SelectedValue);



            talents = talentList.ToArray();

            foreach (var s in talents)
            {
                var en = c.AddTalent(s, this);
                while (en.MoveNext())
                    yield return en.Current;
            }

        }

        public override void DeAcire(Character c)
        {
            foreach (var s in skills)
            {
                c.RemoveSkill(s, this);
            }

            foreach (var item in talents)
            {
                c.RemoveTalent(item, this);
            }
            this.talents = null;
        }

        public override Rank[] Ranks
        {
            get { return new Rank[] { }; }
        }

        public override BackgroundPackage[] BackgroundPackages
        {
            get { return new BackgroundPackage[] { new TheGreatChantriesofTarsus(), new MendicantineMissionary(), new RedemptionistFirebrand() }; }
        }



        #region BackgroundPackges
        [Serializable]
        private class TheGreatChantriesofTarsus : BackgroundPackage
        {

            private Skill[] skills;

            private Talent talent;


            private int insanety;

            public override string Name
            {
                get { return "The Great Chantries of Tarsus"; }
            }

            public override int Cost
            {
                get { return 300; }
            }

            public override bool IsPreconditionSattisfied(Character c)
            {
                return base.IsPreconditionSattisfied(c) && c.HomeWorld is HomeWorlds.NobleBorn || c.HomeWorld is HomeWorlds.ImperialWorld || c.HomeWorld is HomeWorlds.HiveWorld;
            }

            public override IEnumerator<Comunicator> Accire(Character c)
            {
                var en = base.Accire(c);
                while (en.MoveNext())
                    yield return en.Current;

                c.Int += 5;
                c.Fel += 5;
                c.WS -= 5;
                c.BS -= 5;
                c.S -= 5;

                talent = new Talents.Peers.PeerEcclesiarchy();

                en = c.AddTalent(talent, this);
                while (en.MoveNext())
                    yield return en.Current;

                skills = new Skill[] { new Skills.Charm(Skill.SkillLevel.Skilled), new Skills.CommonLores.CommonLoreEcclesiarchy(Skill.SkillLevel.Skilled), new Skills.Scrutiny(Skill.SkillLevel.Skilled), new Skills.SpeakLanguages.SpeakLanguageHighGothic(Skill.SkillLevel.Skilled) };
                foreach (var s in skills)
                {
                    en = c.AddSkill(s, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }

                //TODO Replace Gear

                var rolDie = new RoleDice("Insanaty", Dice.D5);
                while (rolDie.Value == -1)
                    yield return rolDie;
                insanety = rolDie.Value;

                c.InsanityPoints += insanety;
            }

            public override void DeAccire(Character c)
            {
                base.DeAccire(c);
                c.RemoveTalent(talent, this);
                foreach (var s in skills)
                {
                    c.RemoveSkill(s, this);
                }


                c.Int -= 5;
                c.Fel -= 5;
                c.WS += 5;
                c.BS += 5;
                c.S += 5;

                c.InsanityPoints -= insanety;

            }
        }

        [Serializable]
        private class MendicantineMissionary : BackgroundPackage
        {

            private Skill[] skills;

            public override string Name
            {
                get { return "Mendicantine Missionary"; }
            }

            public override int Cost
            {
                get { return 100; }
            }



            public override IEnumerator<Comunicator> Accire(Character c)
            {
                var en = base.Accire(c);
                while (en.MoveNext())
                    yield return en.Current;



                skills = new Skill[] { new Skills.CommonLores.CommonLoreImperium(Skill.SkillLevel.Skilled), new Skills.Navigations.NavigationSurface(Skill.SkillLevel.Skilled), new Skills.ScholasticLores.ScholasticLoreLegend(Skill.SkillLevel.Skilled), new Skills.Survival(Skill.SkillLevel.Skilled) };
                foreach (var s in skills)
                {
                    en = c.AddSkill(s, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }

            }

            public override void DeAccire(Character c)
            {
                base.DeAccire(c);
                foreach (var s in skills)
                {
                    c.RemoveSkill(s, this);
                }


            }
        }


        [Serializable]
        private class RedemptionistFirebrand : BackgroundPackage
        {

            private Skill[] skills;
            private Talent[] talents;
            private int insanety;

            public override string Name
            {
                get { return "Redemptionist Firebrand"; }
            }

            public override int Cost
            {
                get { return 200; }
            }



            public override IEnumerator<Comunicator> Accire(Character c)
            {
                var en = base.Accire(c);
                while (en.MoveNext())
                    yield return en.Current;

                c.Fel -= 5;

                var talentList = new List<Talent>();
                talentList.Add(new Talents.UnshakeabkleFaith());

                var com = new Comunication.ChooseOne<Talent>("Choose One", new Talents.WeaponTraining.BasicWeaponTrainingFlame(), new Talents.WeaponTraining.MeleeWeaponTrainingChain());
                while (com.SelectedValue == null)
                    yield return com;
                talentList.Add(com.SelectedValue);

                talents = talentList.ToArray();

                skills = new Skill[] { new Skills.Interrogation(Skill.SkillLevel.Skilled), new Skills.Intimidate(Skill.SkillLevel.Skilled) };
                foreach (var s in skills)
                {
                    en = c.AddSkill(s, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }
                foreach (var s in talents)
                {
                    en = c.AddTalent(s, this);
                    while (en.MoveNext())
                        yield return en.Current;
                }

                var rolDie = new RoleDice("Insanaty", Dice.D5);
                while (rolDie.Value == -1)
                    yield return rolDie;
                insanety = rolDie.Value;

                c.InsanityPoints += insanety;

            }

            public override void DeAccire(Character c)
            {
                base.DeAccire(c);
                c.Fel += 5;

                foreach (var s in skills)
                {
                    c.RemoveSkill(s, this);
                }
                foreach (var s in talents)
                {
                    c.RemoveTalent(s, this);
                }

                c.InsanityPoints -= insanety;
            }
        }
        #endregion


        public override int[] WeaponSkillCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] BalisticSkillCost
        {
            get { return new int[] { 100, 250, 500, 750 }; }
        }

        public override int[] StrengthCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] ToughnessCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] AgilityCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] IntelligenceCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] PerceptionCost
        {
            get { return new int[] { 250, 500, 750, 1000 }; }
        }

        public override int[] WillpowerCost
        {
            get { return new int[] { 100, 250, 500, 750 }; }
        }

        public override int[] FellowshipCost
        {
            get { return new int[] { 100, 250, 500, 750 }; }
        }
    }
}
